package com.forum.LoginServlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.forum.controller.MainController;
import com.forum.domain.User;
import com.forum.domain.UserDAO;

public class Login extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Login() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		Cookie[] cookies = request.getCookies();

		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String rememberme = request.getParameter("rememberme");
		UserDAO userDao = new UserDAO();
		if (userDao.findByEmail(email).size() == 0) {
			out.println("no user");
			return;
		}

		User user = (User) userDao.findByEmail(email).get(0);

		if (user.getPassword().equals(password) == false) {
			out.println("wrong password");
			return;
		}

		/*
		 * if (rememberme.equals("true")) { Cookie cookie = new Cookie("email",
		 * user.getEmail()); cookie.setMaxAge(60 * 60 * 24 * 7);
		 * response.addCookie(cookie); }
		 */

		// request.getSession().setAttribute("controller", new
		// MainController(user.getUserid()));
		request.getSession().setAttribute("login", "true");
		request.getSession().setAttribute("id", user.getUserid());
		out.println("correct");
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		System.out.println("fdsfasdsfs");

		Cookie[] cookies = request.getCookies();

		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String rememberme = request.getParameter("rememberme");
		UserDAO userDao = new UserDAO();
		User user = (User) userDao.findByEmail(email).get(0);
		if (user.getPassword().equals(password) == true) {
			request.getSession().setAttribute("login", "true");
			request.getSession().setAttribute("id", user.getUserid());
			response.sendRedirect("page/home.jsp");
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
